// pages/service/withdraw-money/index.js
var app = getApp(), api = require('../../../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      
  }, 
  onShow: function(){
    var that = this;
    app.request({
      url: api.order.balance,
      data: {
        uid: wx.getStorageSync('uid')
      },
      success: function (e) {
        if (e.code == 1) {
          that.setData({
            money: e.data
          })
        }
      }
    })
    app.request({
      url: api.card.CardDefault,
      data:{
        uid:wx.getStorageSync('uid')
      },
      success:function(e){
        if(e.code==1){
          that.setData({
            data: e.data
          })
          
        }
        
      }
    })
    // var cash = wx.getStorageSync('cash');
    // app.request({
    //   url: api.user.bank,
    //   method: 'post',
    //   data: { 
    //       cid: cash.cid,
    //       uid: wx.getStorageSync('uid'),
    //       bid: wx.getStorageSync('bid')
    //     },
    //   success: function (e) {
    //     if(e.code==1){
    //       that.setData({
    //         data: e.data
    //       })
    //     }else if(e.code ==0){
    //       wx.showModal({
    //         title: '提示',
    //         content: '未设置银行卡',
    //         success: function (res) {
    //           if (res.confirm) {
    //             wx.navigateTo({
    //               url: '/sd_liferuning/pages/service/bankcard-binding/index',
    //             })
    //           } else if (res.cancel) {
    //             wx.navigateTo({
    //               url: '/sd_liferuning/pages/service/balance-management/index',
    //             })
    //           }
    //         }
    //       })  
    //     }else{
    //       wx.showToast({
    //         title: '网络错误！！',
    //         icon: 'loading',
    //         duration: 2000
    //       }) 
    //     }
    //   }
    // })
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "balance") {
      that.setData({
        balance: inputVlaue
      });
    }
  },
  sendRequest: function () {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var bid = wx.getStorageSync('bid');
    var money = that.data.balance;
    if (that.validate()) {
      // TODO 请求
      app.request({
        url: api.outprice.out,
        method: 'post',
        data: {
          uid: uid,
          bid:bid,
          money: money
        },
        success: function (res) {
        
          if (res.code==1){
            wx.showModal({
              title: '提示',
              content: '提现申请成功',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/sd_liferuning/pages/constmer/balance-management/index',
                  })
                } else if (res.cancel) {
                  wx.navigateTo({
                    url: '/sd_liferuning/pages/constmer/withdraw-money/index',
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel:false
            })
          }  
        }
      })

    }
  },
  validate: function () {
    var that = this;
    var balance = that.data.balance;
    var money = that.data.money;
    if (!that.data.balance || that.data.balance < 1) {
      wx.showModal({
        title: '提示',
        content: '取现金额不足1元',
        showCancel:false
      })
    
      return false;
    }

    if (!that.data.balance || Number(balance) > Number(money)) {
      wx.showModal({
        title: '提示',
        content: '取现余额不足',
        showCancel: false
      })

    
      return false;
    }
    return true;
  },
  click:function(){
    console.log('111');
    wx.navigateTo({
      url: "/sd_liferuning/pages/constmer/bankcard-list/index",
    })
  }
})